﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Web.Configuration;
using Domain;
using System.IO;

namespace HotelBrisaYArenaAdministrador
{
    public partial class EditarFacilidadSeleccionada : System.Web.UI.Page
    {
        ImagenBusiness imagenBusiness = new ImagenBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        PaginaBusiness paginaBusiness = new PaginaBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        SeccionBusiness seccionBusiness = new SeccionBusiness(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        int idImagen;
        Imagen imagen = new Imagen();
        Seccion seccion;
        Pagina pagina;
        int idSeccion, idPagina;

        protected void Page_Load(object sender, EventArgs e)
        {
           
           
            idSeccion = Int32.Parse(Request.QueryString["valor"].ToString());

            seccion = paginaBusiness.GetPaginaForSeccion(idSeccion);

            if (!Page.IsPostBack)
            {
                TextBox1.Text = seccion.Descripcion;
            }

           
            imagen = imagenBusiness.GetImagen(seccion.IdImagen);
            byte[] imageBytes = imagen.Image;
            string base64ImageString = ConvertBytesToBase64(imageBytes);
            Image1.Src = "data:image/jpg;base64," + base64ImageString;
        }

        public string ConvertBytesToBase64(byte[] imageBytes)
        {
            return Convert.ToBase64String(imageBytes);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String descripcion = TextBox1.Text;
            if (FileUpload1.HasFile)
            {
                using (BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream))
                {
                    byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);
                    idImagen = imagenBusiness.GuardarImagen(image);
                    imagenBusiness.ActualizarImagenSeccion(image, 3, idSeccion);
                }
            }
            try
            {
                seccionBusiness.ActualizarDescripcionSeccion(descripcion, 3, idSeccion);
            }
            catch (Exception)
            { }
            Response.Redirect("PaginaPrincipal.aspx");
        }

    }
}